<?php
/**
 * Created by PhpStorm.
 * User: Huyang
 * Date: 2016/9/20
 * Time: 13:28
 */

namespace App\Http\Requests\Api;

use App\Exceptions\ServiceException;
use App\Http\Constants\CommonExceptionConstants;
use App\Http\Repositories\Daos\IUserDao;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class UserContext
{

    private $id;

    private $name;

    private $mobile;

    private $level;

    private $avatar;

    private $recommendId;

    private $remoteIp;

    public function __construct(Request $request, IUserDao $userDao)
    {

        //模拟支付用户ID
        if (!$request->userId) {
            throw new ServiceException(CommonExceptionConstants::getKey('no_login'));
        }

        Redis::set('User:'.$request->userId, null);

        $data = Redis::get('User:'.$request->userId);

        if ($data) {
            $userInfo = unserialize($data);
        } else {
            //更新用户信息
            $userInfo = $userDao->find($request->userId);
            if ($userInfo) {
                $userInfo = $userInfo->toArray();
                Redis::set('User:'.$request->userId, serialize($userInfo));
            } else {
                throw new ServiceException(CommonExceptionConstants::getKey('no_login'));
            }
        }

        $this->setId($userInfo['id']);
        $this->setAvatar($userInfo['avatar']);
        $this->setLevel($userInfo['level']);
        $this->setName($userInfo['nickname']);
        $this->setMobile($userInfo['mobile']);
        $this->setRecommendId(intval($userInfo['recommend_id']));

    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getMobile()
    {
        return $this->mobile;
    }

    /**
     * @param mixed $mobile
     */
    public function setMobile($mobile)
    {
        $this->mobile = $mobile;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getLevel()
    {
        return $this->level;
    }

    /**
     * @param mixed $level
     */
    public function setLevel($level)
    {
        $this->level = $level;
    }

    /**
     * @return mixed
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * @param mixed $avatar
     */
    public function setAvatar($avatar)
    {
        $this->avatar = $avatar;
    }

    /**
     * @return mixed
     */
    public function getRecommendId()
    {
        return $this->recommendId;
    }

    /**
     * @param mixed $recommendId
     */
    public function setRecommendId($recommendId)
    {
        $this->recommendId = $recommendId;
    }

    /**
     * @return mixed
     */
    public function getRemoteIp()
    {
        return $this->remoteIp;
    }

    /**
     * @param mixed $remoteIp
     */
    public function setRemoteIp($remoteIp)
    {
        $this->remoteIp = $remoteIp;
    }

}